package com.gw.quotes.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.gw.quotes.R;
import com.gw.quotes.entity.business.SearchPriceList.PriceList;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

public class SearchAdapter extends BaseAdapter {

	private List<PriceList> list;
	private Context context;
	private int resource;
	private ImageLoader imageLoader;
	private DisplayImageOptions options;

	public SearchAdapter(Context context, int layoutId, List<PriceList> list) {
		this.context = context;
		this.resource = layoutId;
		this.list = list;
		this.imageLoader = ImageLoader.getInstance();
		this.imageLoader.init(ImageLoaderConfiguration.createDefault(context));
		this.options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_empty)
				.showImageForEmptyUri(R.drawable.ic_empty)
				.showImageOnFail(R.drawable.ic_empty).cacheInMemory(true)
				.cacheOnDisk(true).displayer(new FadeInBitmapDisplayer(500))// 是否图片加载好后渐入的动画时间
				.bitmapConfig(Bitmap.Config.RGB_565).build();
	}

	@Override
	public int getCount() {
		return this.list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		PriceList priceList = list.get(position);
		RelativeLayout layout = null;
		if (convertView == null) {
			layout = (RelativeLayout) LayoutInflater.from(this.context)
					.inflate(resource, null);
		} else {
			layout = (RelativeLayout) convertView;
		}
		TextView goods_title = (TextView) layout
				.findViewById(R.id.search_title);
		TextView goods_price = (TextView) layout
				.findViewById(R.id.search_price);
		TextView goods_price2 = (TextView) layout
				.findViewById(R.id.search_price2);
		ImageView goods_img = (ImageView) layout.findViewById(R.id.search_img);
		imageLoader.displayImage(priceList.getImgurl(), goods_img, options);
		goods_title.setText(priceList.getGoodsname());

		if (priceList.getMarketprice() != null
				&& priceList.getMarketprice().intValue() != 0) {
			goods_price2.setVisibility(View.VISIBLE);
			goods_price2.setText(String.format("￥%.2f",
					priceList.getMarketprice()));
			goods_price2.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
		} else {
			goods_price2.setVisibility(View.GONE);
		}
		goods_price.setText(String.format("￥%.2f",
				priceList.getAgreementprice()));
		return layout;
	}
}
